package com.huobi.pro.market.client;

import com.alibaba.fastjson.JSONObject;
import com.huobi.HuobiProperty;
import com.huobi.pro.market.ApiSignature;
import com.huobi.pro.market.HuobiWsClient;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.TreeMap;

@Component
public class ClientAuth {
    @Autowired
    private ApiSignature apiSignature;
    @Autowired
    HuobiProperty property;

    public void sendAuth(HuobiWsClient huobiWsClient, String path) {
        HuobiProperty p = huobiWsClient.getHuoboProperty();
        Map<String, String> map = new TreeMap<>();
        apiSignature.createSignature(p.getAccessKey(),p.getSecretKey(),p.getHost(),"GET", path, map);
        JSONObject msg = new JSONObject();
        map.entrySet().forEach(entry -> {
            msg.put(entry.getKey(), entry.getValue());
        });
        msg.put(ApiSignature.op, ApiSignature.opValue);
        msg.put("type", "api");
        msg.put("cid", RandomStringUtils.randomAlphanumeric(10).toLowerCase());
        huobiWsClient.send(msg.toString());
    }

}
